package conchmusic.conchmusicclient.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import conchmusic.conchmusicclient.R;
import conchmusic.conchmusicclient.dao.ClientControl;
import conchmusic.conchmusicclient.dao.MyDBHelp;
import conchmusic.conchmusicclient.pojo.Song;
import conchmusic.conchmusicclient.pojo.SongList;
import conchmusic.conchmusicclient.thread.InsertSongToSonglistThread;

public class AddtoOtherList extends AppCompatActivity {

    private ListView lv_otherlist;
    private Button cancel;
    private List<SongList> listsong;
    private ClientControl cc;
    private MyDBHelp myDBHelp;
    private int userId;
    private MylistAdapter mylistAdapter;
    private int songlistId;
    private int songId;
    private InsertSongToSonglistThread insertSongToSonglistThread;


    private List<Song> songs;
    private boolean isIn;

    public  Handler getHandler() {
        return handler;
    }

    private  Handler handler;
    private static final int INSERT_COMPLETE=3;
    private static final int INSERT_FAIL=1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addto_other_list);


        myDBHelp = new MyDBHelp(AddtoOtherList.this, "demon", null, 1);
        Bundle bundle=new Bundle();
        final Intent intent=getIntent();
        bundle=intent.getExtras();
        userId=bundle.getInt("userId2");
        songlistId=bundle.getInt("songListId2");
        songId=bundle.getInt("songId");
        Log.i("。。。。。。收到歌单id为",songlistId+"");
        myDBHelp = new MyDBHelp(this, "demon", null, 1);
        lv_otherlist=(ListView)findViewById(R.id.id_otherlist);
        cc=new ClientControl(myDBHelp);
        listsong=cc.getSongLists(userId);
//      去掉歌单列表中歌单本身
        for(int i=0;i<listsong.size();i++){
            if(listsong.get(i).getSongListId()==songlistId){
                listsong.remove(i);
            }
        }
        mylistAdapter=new MylistAdapter(listsong);
        lv_otherlist.setAdapter(mylistAdapter);
        cancel=(Button)findViewById(R.id.addcancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case INSERT_COMPLETE:

                        Toast.makeText(AddtoOtherList.this,"成功加入歌单",Toast.LENGTH_SHORT).show();
                        onBackPressed();
                        break;

                    case INSERT_FAIL:

                        Toast.makeText(AddtoOtherList.this,"加入失败",Toast.LENGTH_SHORT).show();
                        onBackPressed();
                        break;
                }
            }
        };
        lv_otherlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                isIn=false;
                int playlistId=listsong.get(position).getSongListId();
                songs=cc.findSongs(playlistId);
                for(int i=0;i<songs.size();i++){
                    if(songId==songs.get(i).getSongId()){
                        isIn=true;
                    }
                }
                if(isIn){
                    Toast.makeText(AddtoOtherList.this,"歌曲已存在",Toast.LENGTH_SHORT).show();
                }else {
                    insertSongToSonglistThread=new InsertSongToSonglistThread(playlistId,songId,AddtoOtherList.this,userId,handler);
                    Thread thread=new Thread(insertSongToSonglistThread);
                    thread.start();
                }

            }
        });

    }




    public class MylistAdapter extends BaseAdapter {
        private Context context;
        private List<SongList> data;
        public MylistAdapter(List<SongList> data){
            this.data=data;
        }

        @Override
        public int getCount() {
            return data == null ? 0 : data.size();
        }
        @Override
        public Object getItem(int i) {
            return data.get(i);
        }


        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder = null;
            if(context == null)
                context = viewGroup.getContext();
            if(view == null){
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.add_to_list,null);
                viewHolder = new ViewHolder();
                viewHolder.mmTv = (TextView)view.findViewById(R.id.mmTv);
                view.setTag(viewHolder);
            }
            viewHolder = (ViewHolder)view.getTag();
            //
            viewHolder.mmTv.setText(data.get(i).getSongListName());
            //设置tag标记
            viewHolder.mmTv.setTag(R.id.tv,data.get(i).getSongListId());//添加此代码
            return view;
        }
//
        class ViewHolder{
            TextView mmTv;
        }
    }

}
